package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.videoplayer.JZVideoPlayer;
import com.dingwei.videoplayer.JZVideoPlayerSimple;
import com.dingwei.videoplayer.JZVideoPlayerStandard;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class VideoPlayerActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.videoplayer)
    JZVideoPlayerStandard videoplayer;
    private String url = "";//视频地址
    private String face = "";//封面图
    private String title = "";//标题
    int type;//1 本地文件


    @Override
    protected int getContentViewId() {
        return R.layout.activity_video_player;
    }

    @Override
    public void initData() {
        videoplayer.setUp(url
                , JZVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, title);
        //设置封面
        if (type > 0) {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(url);
            Bitmap bitmap = mmr.getFrameAtTime();
            videoplayer.thumbImageView.setImageBitmap(bitmap);
        } else {
            Glide.with(this).load(face).into(videoplayer.thumbImageView);
        }

        videoplayer.backButton.setVisibility(View.VISIBLE);
        videoplayer.batteryTimeLayout.setVisibility(View.GONE);
        videoplayer.backButton.setOnClickListener(this);
        videoplayer.fullscreenButton.setOnClickListener(this);
    }

    @Override
    public void initView() {

        Intent intent = getIntent();
        title = intent.getStringExtra("name");
        face = intent.getStringExtra("face");
        url = intent.getStringExtra("url");
        type = intent.getIntExtra("type", 0);
        if (TextUtils.isEmpty(title)) {
            title = "";
        }
        if (TextUtils.isEmpty(face)) {
            face = "";
        }
        if (TextUtils.isEmpty(url)) {
            url = "";
        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    public void closeCheck() {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            finish();
//            overridePendingTransition(R.anim.back_anim_left, R.anim.back_anim_right);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                closeCheck();
                break;
            case R.id.fullscreen:
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
                break;
            default:
                break;
        }
    }

    private Handler handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE || getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        handle.sendEmptyMessageDelayed(0, 3000);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
